package com.example.ftp.View.MainWindow.FileList;

import com.example.ftp.View.MainWindow.MainWindow;
import javafx.scene.control.*;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;
import java.util.Optional;

public class FileItemContextMenu extends ContextMenu {

    public FileItemContextMenu() {
        FileItemContextMenuItem renameItem = new FileItemContextMenuItem("重命名", "Please enter the new name", "Rename");
//        FileItemContextMenuItem moveItem = new FileItemContextMenuItem("Move", "Please enter the new path", "Move");
        FileItemContextMenuItem downloadItem = new FileItemContextMenuItem("下载",
                "Please choose the local directory", "download");
        FileItemContextMenuItem uploadItem = new FileItemContextMenuItem("上传",
                "Please choose the local file", "upload");
        FileItemContextMenuItem newDirItem = new FileItemContextMenuItem("新建",
                "Please enter the name", "New Directory");
        FileItemContextMenuItem deleteItem = new FileItemContextMenuItem("删除文件",
                "Are your sure to delete this file?", "Delete File");

        getItems().add(renameItem);
//        getItems().add(moveItem);
        getItems().add(downloadItem);
        getItems().add(uploadItem);
        getItems().add(newDirItem);
        getItems().add(deleteItem);


        renameItem.setOnAction(e -> {
            // 接收新的名字
            TextInputDialog dialog = new TextInputDialog();
            dialog.setTitle(renameItem.getOperation());
            dialog.setHeaderText(renameItem.getHint());
            Optional<String> result = dialog.showAndWait();

            // 更名
            result.ifPresent(s -> {
                MainWindow.getControllerInstance().renameFile(s);
                MainWindow.getControllerInstance().updateFileList();
            });
        });

        newDirItem.setOnAction(e -> {
            // 接收新的名字
            TextInputDialog dialog = new TextInputDialog();
            dialog.setTitle(newDirItem.getOperation());
            dialog.setHeaderText(newDirItem.getHint());
            Optional<String> result = dialog.showAndWait();

            // 创建文件夹
            result.ifPresent(s -> {
                MainWindow.getControllerInstance().mkdir(s);
            });
        });

        downloadItem.setOnAction(e -> {
            // 选择本地路径
            DirectoryChooser chooser = new DirectoryChooser();
            chooser.setTitle(downloadItem.getHint());
            File target = chooser.showDialog(new Stage());
            // 下载
            if (target != null)
                MainWindow.getControllerInstance().downloadFile(target.getAbsolutePath());
        });

        uploadItem.setOnAction(e -> {
            // 选择本地路径
            FileChooser chooser = new FileChooser();
            chooser.setTitle(uploadItem.getHint());
            File target = chooser.showOpenDialog(new Stage());
            // 上传
            if (target != null)
                MainWindow.getControllerInstance().uploadFile(target.getAbsolutePath());
        });

        deleteItem.setOnAction(e -> {
            Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
            alert.setTitle(deleteItem.getOperation());
            alert.setHeaderText("Confirm");
            alert.setContentText("Are you sure you want to delete it? ");

            Optional<ButtonType> result = alert.showAndWait();
            result.ifPresent(s -> {
                if (result.get() == ButtonType.OK){
                    MainWindow.getControllerInstance().deleteFile();
                }
            });
        });
    }
}
